package Z_directExchange;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author： pang-yun
 * @date： 2021-12-20 10:28
 * 功能目的 ：用于
 */

public class DirectConsumerB {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 拿到信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(Const.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明队列
        channel.queueDeclare(Const.QUEUE_NAME_THIRD,false,false,false,null);
        // 交换机与队列绑定
        channel.queueBind(Const.QUEUE_NAME_THIRD,Const.EXCHANGE_NAME,Const.ROUTE_KEY_C);

        //=============回调 函数
        // 成功
        DeliverCallback deliverCallback =((consumerTag, message) -> {
            System.out.println("接收消息: " + new String(message.getBody()));
        });
        //失败
        CancelCallback cancelCallback =consumerTag -> {
            System.out.println("接收消息失败");
        };

        channel.basicConsume(Const.QUEUE_NAME_THIRD,deliverCallback,cancelCallback);

    }
}
